﻿#region Copyright (C) 2008, 2009 StreamTv

/* 
 *      Copyright (C) 2008, 2009 StreamTv
 *      http://code.google.com/p/mpstreamtv/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#endregion

#region Imports
using System;
using System.Net;
#endregion

namespace StreamTv2
{
    /// <summary>
    /// Class to simplify error message replies.
    /// </summary>
    internal static class HttpResponses
    {
        #region Public methods
        #region Generic Client Responses
        /// <summary>
        /// Send to the client a error message saying that Server Is Busy.
        /// </summary>
        /// <param name="clientContext">Client to inform.</param>
        public static void ServerBusy(ref HttpListenerContext clientContext)
        {
            // Send server busy.
            HttpListenerResponse response = clientContext.Response;
            try
            {
                // Construct a response.
                response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
                response.StatusDescription = "Service Unavailable";
                response.ProtocolVersion = HttpVersion.Version10;
                response.KeepAlive = false;
                response.ContentLength64 = 0;
            }
            catch (Exception)
            {
            }
            finally
            {
                // Tell the listener object to send the buffer and close off the connection.
                response.Close();
            }
        } // ServerBusy

        /// <summary>
        /// Send to the client a error message saying that Not Implemented.
        /// </summary>
        /// <param name="clientContext">Client to inform.</param>
        public static void NotImplemented(ref HttpListenerContext clientContext)
        {
            // Send not implemented response.
            HttpListenerResponse response = clientContext.Response;
            try
            {
                // Construct a response.
                response.StatusCode = (int)HttpStatusCode.NotImplemented;
                response.StatusDescription = "Not Implemented";
                response.ProtocolVersion = HttpVersion.Version10;
                response.KeepAlive = false;
                response.ContentLength64 = 0;
            }
            catch (Exception)
            {
            }
            finally
            {
                // Tell the listener object to send the buffer and close off the connection.
                response.Close();
            }
        } // NotImplemented

        /// <summary>
        /// Send to the client a error message saying that File Not Found.
        /// </summary>
        /// <param name="clientContext">Client to inform.</param>
        public static void NotFound(ref HttpListenerContext clientContext)
        {
            // Send not found.
            HttpListenerResponse response = clientContext.Response;
            try
            {
                // Construct a response.
                response.StatusCode = (int)HttpStatusCode.NotFound;
                response.StatusDescription = "Not Found";
                response.ProtocolVersion = HttpVersion.Version10;
                response.KeepAlive = false;
                response.ContentLength64 = 0;
            }
            catch (Exception)
            {
            }
            finally
            {
                // Tell the listener object to send the buffer and close off the connection.
                response.Close();
            }
        } // NotFound

        /// <summary>
        /// Send to the client a error message saying that Internal Server Error.
        /// </summary>
        /// <param name="clientContext">Client to inform.</param>
        public static void InternalServerError(ref HttpListenerContext clientContext)
        {
            // Send internal server error.
            HttpListenerResponse response = clientContext.Response;
            try
            {
                // Construct a response.
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                response.StatusDescription = "Internal Server Error";
                response.ProtocolVersion = HttpVersion.Version10;
                response.KeepAlive = false;
                response.ContentLength64 = 0;
            }
            catch (Exception)
            {
            }
            finally
            {
                // Tell the listener object to send the buffer and close off the connection.
                response.Close();
            }
        } // InternalServerError
        #endregion
        #endregion
    }
}
